

-- Namespace
	local _,mod = ...


-- Object ref
	local RAID_CLASS_COLORS = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS


-- Constants
	local font = {mod.path..'media\\bavaria.ttf',8,'OUTLINEMONOCHROME'}
	
	local c_bar =    {.16,.16,.16}
	local c_barbg =  {.33,.33,.33}
	local c_border = {.07,.07,.07}
	
	local white = { r = 1, g = 1, b = 1 }


-- Helpers
	local function dim(color,perc)
		local r,g,b = color.r,color.g,color.b
		r,g,b = r*perc,g*perc,b*perc
		return r,g,b
	end


for i = 1,5 do
	local unit = ('arena%d'):format(i)
	
	-- Base frame
		local arenaN = mod:UF(unit)
			arenaN:SetFrameLevel(2)
			arenaN:SetHeight(16) arenaN:SetWidth(32)
			arenaN:SetPoint('TOPRIGHT',UIParent,'CENTER',382,64-((i-1)*32))
			
			arenaN:SetScript('OnEnter',function(self)
				GameTooltip_SetDefaultAnchor(GameTooltip,self)
				GameTooltip:SetUnit(self.unit)
			end)
			arenaN:SetScript('OnLeave',function(self)
				GameTooltip:ClearLines()
				GameTooltip:Hide()
			end)
		
		local bg = arenaN:CreateTexture(nil,'BACKGROUND')
			bg:SetAllPoints(true)
			bg:SetTexture(unpack(c_border))
			arenaN.bg = bg


	-- Health
		local hpbar = arenaN:CreateTexture(nil,'ARTWORK')
			hpbar:SetHeight(11) hpbar:SetWidth(15)
			hpbar:SetPoint('TOPRIGHT',-16,-1)
			hpbar:SetTexture(unpack(c_bar))
			hpbar:Listen('health',function(self,min,max)
				local perc = min / max
				if perc <= 0 then
					perc = .001
				elseif perc > 1 then
					perc = 1
				end
				self:SetWidth(15*perc)
			end)
			arenaN.hpbar = hpbar
		
		local inchealbar = arenaN:CreateTexture(nil,'ARTWORK')
			inchealbar:SetHeight(11) inchealbar:SetWidth(15)
			inchealbar:SetPoint('RIGHT',hpbar,'LEFT')
			inchealbar:SetTexture(.2,.6,.2)
			inchealbar:Hide()
			inchealbar:Listen('incheal',function(self,heal,min,max)
				if heal then
					self:Show()
					local def = 1 - (min / max)
					local perc = heal / max
					if perc > def then
						perc = def
					end
					if perc <= 0 then
						perc = .001
					elseif perc > 1 then
						perc = 1
					end
					self:SetWidth(15*perc)
				else
					self:Hide()
				end
			end)
			arenaN.inchealbar = inchealbar
		
		local hpbg = arenaN:CreateTexture(nil,'BORDER')
			hpbg:SetHeight(11) hpbg:SetWidth(15)
			hpbg:SetPoint('TOPRIGHT',-16,-1)
			hpbg:SetTexture(unpack(c_barbg))
			arenaN.hpbg = hpbg


	-- Power
		local ppbar = arenaN:CreateTexture(nil,'ARTWORK')
			ppbar:SetHeight(2) ppbar:SetWidth(15)
			ppbar:SetPoint('BOTTOMRIGHT',-16,1)
			ppbar:SetTexture(1,1,1)
			ppbar:Listen('power',function(self,min,max)
				local perc = min / max
				if perc <= 0 then
					perc = .001
				elseif perc > 1 then
					perc = 1
				end
				self:SetWidth(15*perc)
			end)
			ppbar:Listen('class',function(self,_,enclass)
				local color = RAID_CLASS_COLORS[enclass] or white
				self:SetTexture(dim(color,.75))
			end)
			arenaN.ppbar = ppbar
		
		local ppbg = arenaN:CreateTexture(nil,'BORDER')
			ppbg:SetHeight(2) ppbg:SetWidth(15)
			ppbg:SetPoint('BOTTOMRIGHT',-16,1)
			ppbg:SetTexture(unpack(c_barbg))
			arenaN.ppbg = ppbg
	
	
	-- Class
		local classicon = arenaN:CreateTexture(nil,'OVERLAY')
			classicon:SetPoint('TOPRIGHT',-1,-1)
			classicon:SetPoint('BOTTOMRIGHT',-1,1)
			classicon:SetWidth(arenaN:GetHeight()-2)
			classicon:SetTexture(0,1,0,.5)
			classicon:Listen('unit',function(self,exists)
				local _,itype = IsInInstance()
				
				if itype == 'pvp' then
					local team = UnitFactionGroup(unit)
					if team == 'Alliance' then
						self:SetTexture('Interface\\Glues\\CHARACTERCREATE\\UI-CharacterCreate-Factions')
						self:SetTexCoord(.05,.45,.05,.95)
						return
					elseif team == 'Horde' then
						self:SetTexture('Interface\\Glues\\CHARACTERCREATE\\UI-CharacterCreate-Factions')
						self:SetTexCoord(.55,.95,.05,.95)
						return
					end
					
				elseif itype == 'arena' then
					local spec = GetArenaOpponentSpec(i)
					if spec and spec > 0 then
						local _,_,_,sicon = GetSpecializationInfoByID(spec)
						self:SetTexture(sicon)
						self:SetTexCoord(.05,.95,.05,.95)
						return
					end
					
				end
				
				self:SetTexture('Interface\\Icons\\INV_Misc_QuestionMark')
				self:SetTexCoord(.05,.95,.05,.95)
			end)
			arenaN.classicon = classicon
end
